<template>
  <el-row type="flex"
          justify="center">
    <el-col :xl="6"
            :lg="7">
      <h2>vue权限管理</h2>
      <el-image :src="require('@/assets/logo.png')"
                style="width: 180px; height: 180px" />
      <p>一个基于sprigboot,vue的权限管理系统</p>
    </el-col>
    <el-col :span="1">
      <el-divider direction="vertical"></el-divider>
    </el-col>
    <el-col :xl="6"
            :lg="7">
      <el-form :model="loginForm"
               :rules="rules"
               ref="loginForm"
               label-width="100px"
               class="demo-ruleForm">
        <el-form-item label="用户名"
                      prop="username"
                      style="width: 380px">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码"
                      prop="password"
                      style="width: 380px">
          <el-input v-model="loginForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="验证码"
                      prop="captcha"
                      style="width: 380px">
          <el-input v-model="loginForm.captcha"
                    style="width: 142px; float: left"></el-input>
          <el-image :src="captchaImg"
                    @click="getCaptcha"
                    class="captcha" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     @click="submitForm('loginForm')">登录</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<script>
import qs from 'qs'
export default {
  name: 'login',
  components: {},
  props: {},
  data() {
    return {
      loginForm: {
        username: null,
        password: null,
        captcha: null,
        capToken: null,
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'change' },
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
        ],
        captcha: [{ max: 5, min: 5, message: '验证码格式错误' }],
      },
      captchaImg: null,
    }
  },
  created() {
    this.getCaptcha()
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) {
          return
        }
        this.$axios
          .post('/login?' + qs.stringify(this.loginForm))
          .then((res) => {
            let jwt = res.headers['authorization']
            this.$store.commit('SET_TOKEN', jwt)
            this.$message.success('登录成功')
            this.$router.push('/')
          })
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    getCaptcha() {
      this.$axios.get('/getCaptcha').then((res) => {
        this.loginForm.captcha = null
        this.loginForm.capToken = res.data.data.capToken
        this.captchaImg = res.data.data.captcha
      })
    },
  },
  watch: {},
  computed: {},
  mounted() {},
}
</script>
<style scoped>
.el-row {
  background-color: #fafafa;
  height: 100%;
  display: flex;
  align-items: center;
  text-align: center;
}
.el-divider {
  height: 200px;
}
.captcha {
  float: left;
  margin-left: 15px;
  border-radius: 4px;
}
</style>
